/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        int ret = 0;
        int cont = 0;
        int kthSmallest(TreeNode* root, int k) {
            cont =  k;
            dfs(root);
            return ret;
        }
        void dfs(TreeNode* root){
            if(cont == 0 || root == nullptr ) return ;
            dfs(root->left);
            cont--;
            if( cont == 0) ret = root->val;
            dfs(root->right);
            return;
        }
    };